﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class BodyMeasureModelRepository:IBodyMeasureModelRepository
    {

        /// <summary>
        /// 构造函数
        /// </summary>

        private readonly AgricultureDbContext context;
        public BodyMeasureModelRepository(AgricultureDbContext context)
        {
            this.context = context;
        }

        /// <summary>
        /// 获取体尺数据
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Bodymeasuremodel>> Bodymeasure()
        {
            return context.Bodymeasuremodels;

        }

        /// <summary>
        /// 获取体尺数据列表
        /// </summary>
        /// <param name="earTag"></param>
        /// <param name="variety"></param>
        /// <param name="sheepRescissionSex"></param>
        /// <returns></returns>
        public async Task<List<Bodymeasuremodel>> BodymeasuremodelList(string earTag, int variety, bool sheepRescissionSex)
        {

            var departs = context.Bodymeasuremodels.Where(c => c.Isdelete == 0).AsQueryable().AsNoTracking();

            if (!string.IsNullOrEmpty(earTag))
            {
                departs = departs.Where(c=>c.EarTag.Contains(earTag));
            }

            if (variety > 0)
            {
                departs = departs.Where(c => c.Variety == variety);
            }
            if (sheepRescissionSex)
            {
                departs = departs.Where(c => c.SheepRescissionSex == sheepRescissionSex);
            }

            return departs.ToList();


        }


        /// <summary>
        /// 删除体尺数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> Delete(params int[] ids)
        {         
            var sheeps = context.Bodymeasuremodels.Where(c => ids.Contains(c.BodyMeasureId));
            foreach (var item in sheeps)
            {
                item.Isdelete = 1;
            }
            return await context.SaveChangesAsync();

        }


        /// <summary>
        /// 添加体尺数据
        /// </summary>
        /// <param name="bodymeasuremodel"></param>
        /// <returns></returns>
        public async Task<int> Add(Bodymeasuremodel bodymeasuremodel)
        {
            context.Bodymeasuremodels.Add(bodymeasuremodel);
            return await context.SaveChangesAsync();
        }



        /// <summary>
        /// 更新体尺数据
        /// </summary>
        /// <param name="bodymeasuremodel"></param>
        /// <returns></returns>
        public async Task<int> Update(Bodymeasuremodel bodymeasuremodel)
        {
            context.Bodymeasuremodels.Update(bodymeasuremodel);
            return await context.SaveChangesAsync();
        }










    }




}
